﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;

namespace QLTV.Forms.KhaiBao
{
    public partial class frmVessel : BaseForm
    {
        public frmVessel()
        {
            InitializeComponent();
           // statusDataBar1.addTextChange(tblVesselBindingSource);
            statusDataBar1.addTextChange(bandedGridView1);
            statusDataBar1.AddControlPrint(gridControl1);
        }

        private void frmVessel_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dsVessel.tblVesselGroup' table. You can move, or remove it, as needed.
            this.tblVesselGroupTableAdapter.Fill(this.dsVessel.tblVesselGroup);
            // TODO: This line of code loads data into the 'dsVessel.tblEngineType' table. You can move, or remove it, as needed.
            this.tblEngineTypeTableAdapter.Fill(this.dsVessel.tblEngineType);
            // TODO: This line of code loads data into the 'dsVessel.tblEngineMaker' table. You can move, or remove it, as needed.
            this.tblEngineMakerTableAdapter.Fill(this.dsVessel.tblEngineMaker);
            // TODO: This line of code loads data into the 'dsVessel.tblVesselType' table. You can move, or remove it, as needed.
            this.tblVesselTypeTableAdapter.Fill(this.dsVessel.tblVesselType);
            // TODO: This line of code loads data into the 'dsVessel.tblFlag' table. You can move, or remove it, as needed.
            this.tblFlagTableAdapter.Fill(this.dsVessel.tblFlag);
            // TODO: This line of code loads data into the 'dsVessel.tblVessel' table. You can move, or remove it, as needed.
            this.tblVesselTableAdapter.Fill(this.dsVessel.tblVessel);

        }

        private void lkFlag_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
            {
                using (frmFlag f = new frmFlag())
                {
                    f.ShowDialog();
                }
                this.tblFlagTableAdapter.Fill(this.dsVessel.tblFlag);
                this.tblVesselTableAdapter.Fill(this.dsVessel.tblVessel);
            }
        }

        private void lkVesselType_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
            {
                using (frmVesselType f = new frmVesselType())
                {
                    f.ShowDialog();
                }
                this.tblVesselTypeTableAdapter.Fill(this.dsVessel.tblVesselType);
                this.tblVesselTableAdapter.Fill(this.dsVessel.tblVessel);
            }
        }

        private void lkEngineMaker_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
            {
                using (frmEngineMaker f = new frmEngineMaker())
                {
                    f.ShowDialog();
                }
                this.tblEngineMakerTableAdapter.Fill(this.dsVessel.tblEngineMaker);
                this.tblVesselTableAdapter.Fill(this.dsVessel.tblVessel);
            }
        }

        private void lkEngineType_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
            {
                using (frmEngineType f = new frmEngineType())
                {
                    f.ShowDialog();
                }
                this.tblEngineTypeTableAdapter.Fill(this.dsVessel.tblEngineType);
                this.tblVesselTableAdapter.Fill(this.dsVessel.tblVessel);
            }
        }

        private void lkGroup_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
            {
                using (frmGroupVessel f = new frmGroupVessel())
                {
                    f.ShowDialog();
                }
                this.tblVesselGroupTableAdapter.Fill(this.dsVessel.tblVesselGroup);
                this.tblVesselTableAdapter.Fill(this.dsVessel.tblVessel);
            }
        }

        private void statusDataBar1_UpdateClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            this.tblVesselBindingSource.EndEdit();
            this.tblVesselTableAdapter.Update(dsVessel.tblVessel);
            this.tblVesselTableAdapter.Fill(this.dsVessel.tblVessel);
            alertControl1.Show(this, rm.GetString("sMessageCaption", myCultureInfo), rm.GetString("sMessageSuccess", myCultureInfo));
        }

        private void statusDataBar1_DeleteClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            if (XtraMessageBox.Show(rm.GetString("sConfirm", myCultureInfo), rm.GetString("sMessageCaption", myCultureInfo), MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                try
                {
                    this.tblVesselBindingSource.RemoveCurrent();
                    this.tblVesselTableAdapter.Update(dsVessel.tblVessel);
                    this.tblVesselTableAdapter.Fill(this.dsVessel.tblVessel);
                }
                catch (SqlException ex)
                {
                    this.tblVesselTableAdapter.Fill(this.dsVessel.tblVessel);
                    if (ex.ErrorCode == -2146232060)
                    {
                        XtraMessageBox.Show(rm.GetString("sDataInUsed", myCultureInfo), rm.GetString("sMessageCaption", myCultureInfo), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        XtraMessageBox.Show(ex.Message, rm.GetString("sMessageCaption", myCultureInfo), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            
        }

        private void statusDataBar1_UndoClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            this.tblVesselBindingSource.CancelEdit();
            this.tblVesselTableAdapter.Fill(this.dsVessel.tblVessel);
        }

        private void statusDataBar1_NewClick(object sender, StatusDataBar.StatusDataBarArgs e)
        {
            this.tblVesselBindingSource.AddNew();
        }

        private void gridControl1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.End) { tblVesselBindingSource.EndEdit(); }
        }

       
    }
}
